#!/usr/bin/env python
# DCOracle2 installer (hah!) 
#

# The goal of this program is to figure out what platform is being used
# and what version of python, to select the proper pre-built binary dco2
# module.

import sys
import string
import os, os.path, shutil

platform = string.upper(sys.platform)
version = string.split(sys.version)[0]
if version[0:3] == "2.1": version = "2.1"
if version[0:3] == "2.2": version = "2.2"
ORACLE_HOME = None

if platform[:5] == "LINUX":
    module = "rh-7.1"
    suffix = ".so"
elif platform[:5] == "SUNOS" or platform[:7] == "SOLARIS":
    module = "solaris-2.7"
    suffix = ".so"
elif platform[:3] == "WIN":
    module = "win32"
    suffix = ".pyd"
    try:
        from win32con import *
        import win32api
        hkey = win32api.RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\ORACLE",
            0,KEY_QUERY_VALUE)
        ORACLE_HOME = win32api.RegQueryValueEx(hkey,"ORACLE_HOME")[0]
        win32api.RegCloseKey(hkey)
    except:
        pass
else:
    print "ERROR: platform unknown"
    print
    print "I couldn't figure out what kind of system you are on.  Because"
    print "of this, maybe you could submit your platform type to the DCO2"
    print "tracker at http://www.zope.org/Members/matt/dco2/Tracker"
    print
    sys.exit(1)

try:
    if ORACLE_HOME is None: ORACLE_HOME = os.environ["ORACLE_HOME"]
except KeyError:
    ORACLE_HOME = None
    print "WARNING: ORACLE_HOME not set"
    print
    print "Hmm... you don't have ORACLE_HOME set in your environment."
    print "Remember, DCOracle2 will require you to set ORACLE_HOME to point"
    print "to the Oracle installation home."
    print
    print "I'll continue, but I can't check your Oracle version.  The"
    print "pre-built binaries need to link against Oracle 8i client libraries"
    print "which are available from Oracle corporation.  You can get further"
    print "information about Oracle availability for your platform from"
    print "http://technet.oracle.com"
    print 

if not version == "1.5.2" and not version ==  "2.1" and not version == "2.2":
    print "ERROR: Python version mismatch" 
    print
    print "The supplied binaries are pre-built against Python 1.5.2 and"
    print "Python 2.1.  You are currently running Python %s." % version
    print "You will need to rebuild DCOracle2 from source to generate"
    print "the C binding module for Oracle."
    print
    sys.exit(2)

cwd = os.getcwd()

me = os.path.join(cwd,"install.py")
if not os.path.exists(me):
    print "ERROR: install location not found"
    print
    print "This install program should run from the DCOracle2 base"
    print "directory.  It looks like it is currently being run from"
    print "somewhere else, as the file %s" % me
    print "was not found."
    print
    sys.exit(3)

sofile = os.path.join(cwd,"binaries","%s-python-%s-dco2%s" % (module,
    version, suffix))
targetdir = os.path.join(cwd,"DCOracle2")
targetfile = os.path.join(targetdir,"dco2%s" % suffix)

if not os.path.exists(sofile):
    print "ERROR: Proper binary not found"
    print
    print "I was unable to locate the binary file I was looking for,"
    print "which was %s" % sofile
    print 
    print "The proper binary file must be copied to"
    print "%s" % targetfile
    print
    sys.exit(4)


if not os.path.exists(targetdir):
    print "ERROR: target directory not found"
    print
    print "The target directory, %s" % targetdir
    print "does not exist!  I cannot install the proper binary file"
    print "     %s" % sofile
    print "in its proper location at %s" % targetfile
    print
    sys.exit(5)

try:
    print "Installing: binary release for Python %s" % version
    #print "Copying %s to %s" % (sofile, targetfile)
    shutil.copy(sofile, targetfile)
except:
    (type, value, traceback) = sys.exc_info()
    print "ERROR: copy failed"
    print
    print "The copy of %s" % sofile
    print "to %s" % targetfile
    print "failed.   The python error raised is:"
    print "%s: %s" % (type, value)
    sys.exit(6)
        

if ORACLE_HOME:
    try:
        print "... test loading DCOracle2 ..."
        import DCOracle2
        print "success!"
    except:
        (type, value, traceback) = sys.exc_info()
        print "ERROR: DCOracle2 failed to load"
        print
        print "The most likely reason DCOracle2 failed to load is a platform"
        print "binary mismatch between your version of Oracle and the"
        print "version of Oracle the pre-built binary was compiled against,"
        print "Oracle 8i.  You can either rebuild DCOracle2 from source, or"
        print "install Oracle 8i client libraries on your computer.  You"
        print "may be able to obtain Oracle client libraries at"
        print "http://technet.oracle.com"
        print
        print "The python error message is:"
        print "%s: %s" % (type, value)
        sys.exit(7)
else:
    print "The install program cannot test the DCOracle2 installation"
    print "without the ORACLE_HOME environment variable being set"
    print

print "COMPLETE: DCOracle2's C bindings to Oracle are now configured"
